package app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.menhoo.toollibrary.common.utils.SdCardUtil;
import com.lidroid.xutils.util.LogUtils;
import com.rey.material.widget.TabPageIndicator;

import custom.CustomViewPager;
import fragment.DownloadList;
import fragment.UploadList;
import mainUI.AppActivity;
import mainUI.AppFragment;
import menhoo.com.i8officelibrary.R;

/**
 * 我的下载
 * Created by yanglei on 15/11/12.
 */

public class MyDownload extends AppActivity implements AppFragment.BackHandledInterface {

    TabPageIndicator tabPageIndicator;

    TextView textViewSdTotalSize;

    TextView textViewSdCurrSize;

    ProgressBar progress;

    CustomViewPager viewPager;

    private Tab[] mItems = new Tab[]{Tab.Download, Tab.Upload};
    private PagerAdapter mPagerAdapter;
    /**
     * 是否刷新下载列表
     */
    private boolean isRefreshDownloadList=false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //
        setContentView(R.layout.my_download);
        try{
            tabPageIndicator = (TabPageIndicator)findViewById(R.id.tabPageIndicator);
            textViewSdTotalSize = (TextView)findViewById(R.id.textViewSdTotalSize);
            textViewSdCurrSize = (TextView)findViewById(R.id.textViewSdCurrSize);
            progress = (ProgressBar)findViewById(R.id.progress);
            viewPager = (CustomViewPager)findViewById(R.id.viewPager);
        }
        catch (Exception e){
            LogUtils.e(e.getMessage()+"");
        }

        setTitle("我的下载");
        mPagerAdapter =new PagerAdapter(getSupportFragmentManager(),mItems);
        viewPager.setAdapter(mPagerAdapter);
        tabPageIndicator.setViewPager(viewPager);
        tabPageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    if (isRefreshDownloadList) {
                        ((DownloadList) mPagerAdapter.mFragments[0]).init();
                        isRefreshDownloadList = false;
                    }
                    if (mPagerAdapter != null && mPagerAdapter.mFragments[0] != null && mPagerAdapter.mFragments[0] instanceof
                            DownloadList) {
                        ((DownloadList) mPagerAdapter.mFragments[0]).hideToolBar();

                    }
                } else if (position == 1) {
                    if (mPagerAdapter != null && mPagerAdapter.mFragments[1] != null && mPagerAdapter.mFragments[1] instanceof
                            UploadList) {
                        ((UploadList) mPagerAdapter.mFragments[1]).hideToolBar();
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshSdCurrSize();
    }

    /**
     * 刷新sd卡的可用大小
     */
    public void refreshSdCurrSize(){
        long total = SdCardUtil.getSDTotalSize();
        long available = SdCardUtil.getSDAvailableSize();
        long use = total-available;
        textViewSdCurrSize.setText(SdCardUtil.formatFileSize(this,use));
        textViewSdTotalSize.setText(SdCardUtil.formatFileSize(this, available));
        progress.setMax(100);
        progress.setSecondaryProgress((int)(use/1.0f/total*100));
    }

    @Override
    public void setSelectedFragment(AppFragment selectedFragment) {

    }

    public enum Tab {

        Upload ("               下载中"),
        Download ("               已下载");
        private final String name;

        private Tab(String s) {
            name = s;
        }

        public boolean equalsName(String otherName){
            return (otherName != null) && name.equals(otherName);
        }

        public String toString(){
            return name;
        }

    }

    private class PagerAdapter extends FragmentStatePagerAdapter {

        Fragment[] mFragments;
        Tab[] mTabs;

        public PagerAdapter(FragmentManager fm, Tab[] tabs) {
            super(fm);
            mTabs = tabs;
            mFragments = new Fragment[mTabs.length];

        }

        @Override
        public Fragment getItem(int position) {
            if(mFragments[position] == null){
                switch (mTabs[position]) {
                    case Upload:
                        mFragments[position] = new UploadList();
                        break;
                    case Download:
                        mFragments[position] = new DownloadList();
                        break;
                }
            }

            return mFragments[position];
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabs[position].toString().toUpperCase();
        }

        @Override
        public int getCount() {
            return mFragments.length;
        }
    }
    /**
     * 是否刷新下载列表
     */
    public void setRefreshDownloadList(boolean IsRefreshDownloadList){
        this.isRefreshDownloadList = IsRefreshDownloadList;
    }

    @Override
    public void onBackPressed() {
        if(mPagerAdapter!=null && mPagerAdapter.mFragments[0]!=null && mPagerAdapter.mFragments[0] instanceof DownloadList){
            if(((DownloadList)mPagerAdapter.mFragments[0]).F){
                ((DownloadList)mPagerAdapter.mFragments[0]).hideToolBar();
                return;
            }
        }
        if(mPagerAdapter!=null && mPagerAdapter.mFragments[1]!=null && mPagerAdapter.mFragments[1] instanceof UploadList){
            if(((UploadList)mPagerAdapter.mFragments[1]).F){
                ((UploadList)mPagerAdapter.mFragments[1]).hideToolBar();
                return;
            }
        }
        super.onBackPressed();
    }
}
